<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input
            v-model="queryParams.customerName"
            placeholder="请输入客户姓名"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="车牌号" prop="plateNo">
          <el-input
            v-model="queryParams.plateNo"
            placeholder="请输入车牌号"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="处理状态" prop="dealStatus">
          <el-select v-model="queryParams.dealStatus" placeholder="请选择处理状态" clearable>
            <el-option
            v-for="dict in dict.type.sys_workorder_deal_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
  
      <el-table v-loading="loading" :data="workorderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户姓名" prop="customerName" />
        <el-table-column label="车牌号" prop="plateNo" ></el-table-column>
        <el-table-column label="处理状态" align="center" prop="dealStatus">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_workorder_deal_status" :value="scope.row.dealStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>
  
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />


    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="客户姓名" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="号牌号码" prop="plateNo">
          <el-input v-model="form.plateNo" placeholder="请输入号牌号码" />
        </el-form-item>
        <el-form-item label="处理状态" prop="dealStatus">
          <el-select v-model="form.dealStatus" placeholder="请选择处理状态">
            <el-option
            v-for="dict in dict.type.sys_workorder_deal_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="form.createTime"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="datetime"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      
    </div>
  </template>
  
  <script>
  import { listWorkorder,addWorkorder,getWorkorder,updateWorkorder,deleteWorkorder } from "@/api/system/workorder";
  
  export default {
    name: "workorder",
    dicts: ['sys_workorder_deal_status'],
    data() {
      return {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 表单校验
        rules: {
          customerName: [
            { required: true, message: "客户名称不能为空", trigger: "blur" }
          ],
          plateNo: [
            { required: true, message: "车牌号不能为空", trigger: "blur" }
          ],
          dealStatus: [
            { required: true, message: "处理状态不能为空", trigger: "blur" }
          ]
        },
        // 表单参数
        form: {},
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 选中数组
        ids: [],
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 日期范围
        dateRange: [],
        // 总条数
        total: 0,
        // 角色表格数据
        workorderList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerName: undefined,
          plateNo: undefined,
          dealStatus: undefined
        },
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询角色列表 */
      getList() {
        this.loading = true;
        listWorkorder(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.workorderList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection){
        this.ids=selection.map(item=>item.pid);
        this.single=selection.length!=1;
        this.multiple=!selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open=true;
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const pid =row.pid || this.ids;
        getWorkorder(pid).then(resp=>{
          this.form=resp.data;
          this.open=true;
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
          const pids=row.pid || this.ids;
          this.$modal.confirm('是否确认删除角色编号为"'+pids+'"的数据项？').then(()=>{ deleteWorkorder(pids);}).then(()=>{
            this.$modal.msgSuccess("删除成功");
            this.getList();
          })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/workorder/export',this.queryParams,`工单统计_${new Date().getTime()}.xlsx`)
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid=>{
            if(valid){
              if(this.form.pid != undefined){
                //修改
                updateWorkorder(this.form).then(resp=>{
                  this.$modal.msgSuccess("修改成功");
                  this.open=false;
                  this.getList();
                })
              }else{
                //新增
                addWorkorder(this.form).then(resp=>{
                this.$modal.msgSuccess("新增成功");
                this.open=false;
                this.getList();
              })
              }
            }
      
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.resetForm("form");
      },
    }
  };
  </script>